#ifndef _DEVICE_H
#define _DEVICE_H

typedef struct __attribute__ ((__packed__)){ 
    uint8_t motor_tem; //单位 摄氏度
    int32_t motor_acc; //单位 dps/s
    int32_t encoder_pos; //单位 脉冲
    int32_t encoder_pos_original; //单位 脉冲
    int32_t encoder_pos_init;  //单位 脉冲
    float motor_spd; //单位dps
    float motor_vol;   //单位 V
    float motor_curr; //单位 A
    float motor_degree; //单位 度
    float abs_angle;  //单位 度
    float CurrKP,CurrKI,SpdKP,SpdKI,PosKP,PosKI;
}Motor_Para;

typedef struct __attribute__ ((__packed__)){
    float dist;
}Stepmotor_Sta;

typedef struct __attribute__ ((__packed__)){
    float servo_head_x;  //单位 角度0-180
    float servo_head_y;
    float servo_fin_left_out;
    float servo_fin_left_in;
    float servo_fin_right_in;
    float servo_fin_right_out;
}Servo_Sta;

typedef struct __attribute__ ((__packed__))
{
	unsigned char ucYear;
	unsigned char ucMonth;
	unsigned char ucDay;
	unsigned char ucHour;
	unsigned char ucMinute;
	unsigned char ucSecond;
	unsigned int usMiliSecond;
}STime;

typedef struct __attribute__ ((__packed__)){
    STime	stcTime;
    float SAcc[3] ;
    float SGyro[3];
    float SAngle[3];
    int  SMag[3];
    int sDStatus[4];
    int SPress;
    float Saltitude;
    int SLonLat[2];
    float SGPSV[3];
    float SQ[4];
}Imu_Sta;

typedef struct __attribute__ ((__packed__)){
    int elec; //电量
    float volt;     //电压
    float curr;     //电流
    int capa;     //额定容量
    float temp;   //温度
    uint8_t curr_dir; //充放电方向
}Battery_Sta;

typedef struct __attribute__ ((__packed__)){
    bool  leak; //漏水检测传感器 
    float temperature; //环境温度传感器
    Battery_Sta battery;//电量传感器
    Imu_Sta imu0;//头部姿态传感器
    Imu_Sta imu1;//重心姿态传感器
}Sensor_Sta;

typedef struct __attribute__ ((__packed__)){
    uint8_t headL;
    uint8_t headH;
    uint8_t len;
    uint8_t cmd;
    Motor_Para motor_sta;
    Stepmotor_Sta stepmotor_sta;
    Servo_Sta servo_sta;
    Sensor_Sta sensor_sta;
    uint8_t crc16L;
    uint8_t crc16H;
}Dev_Sta_Data;


typedef struct __attribute__ ((__packed__)){
	uint8_t frame_header;
	uint8_t id;
	uint8_t length;
	uint8_t cmd;
	uint8_t null;
	uint8_t data[6];
	uint8_t crc16L;
    uint8_t crc16H;
}Motor_Ctr;

typedef struct __attribute__ ((__packed__)){
    float dist;
}Stepmotor_Ctr;

typedef struct  __attribute__ ((__packed__)){
    float servo_head_x;  //单位 角度0-180
    float servo_head_y;
    float servo_fin_left_out;
    float servo_fin_left_in;
    float servo_fin_right_in;
    float servo_fin_right_out;
}Servo_Ctr;

typedef struct __attribute__ ((__packed__)){
    uint8_t headL;
    uint8_t headH;
    uint8_t len;
    uint8_t cmd;
    Motor_Ctr motor_ctr;
    Stepmotor_Ctr stepmotor_ctr;
    Servo_Ctr servo_ctr;
    uint8_t crc16L;
    uint8_t crc16H;
}Host_Ctr_Cmd;

typedef struct __attribute__ ((__packed__)){
    uint8_t leak;
    uint8_t elec;   
}Report_Exc;

#define     JOYSTICK_CMD             0x02U
#define     SHELL_OPEN               0x08U
#define     POWER_ON                 0x011U
#define     POWER_OFF                0x012U

#define NANO_FRAME_HEAD_LOW      0x0D
#define NANO_FRAME_HEAD_HIGH     0x0A

#define SPEED_CLOSEDLOOP_CTR            0xA2
#define MOTOR_SPEED_MAX   1400
#define STEPMOTOR_MAXDIST 12
#define SERVO_HEAD_MAX  135
#define SERVO_HEAD_MIN  45
#define SERVO_FIN_MAX  180
#define SERVO_FIN_MIN  0

#define Servo_Fin_Left_In_init     180;
#define Servo_Fin_Right_In_init     0;

#define NANO_FRAME_LEN          sizeof(Host_Ctr_Cmd)
#define NANO_FRAME_LEN_NOCRC    (sizeof(Host_Ctr_Cmd) -2)

#define DEV_FRAME_LEN          sizeof(Dev_Sta_Data)
#define DEV_FRAME_LEN_NOCRC    (sizeof(Dev_Sta_Data) -2)

#define SHMSIZE 10

#endif